<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{query}}</h2>
      <span class="sub-title">找到 {{total}} 个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              v-for="(item,index) in songList"
              :key="index"
              @click="play(item.id)"
            >
              <td>{{index+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{item.name}}</span>
                    <span class="iconfont icon-mv" v-if="item.mvid!=0" @click.stop="toMv(item.mvid)"></span>
                  </div>
                  <span>{{item.alias[0]}}</span>
                </div>
              </td>
              <td>{{item.artists[0].name}}</td>
              <td>{{item.album.name}}</td>
              <td>{{item.duration}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div class="item" v-for="(item,index) in playlists" :key="index">
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{item.playCount}}</span>
              </div>
              <img :src="item.coverImgUrl" alt />
              <span class="iconfont icon-play" @click="toMusicList(item.id)"></span>
            </div>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" v-for="(item,index) in mvs" :key="index">
            <div class="img-wrap">
              <img :src="item.cover" alt />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play"></div>
                <div class="num">{{item.playCount}}</div>
              </div>
              <span class="time">{{item.duration}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{item.name}}</div>
              <div class="singer">{{item.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
    ></el-pagination>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "result",
  data() {
    return {
      activeIndex: "songs",
      page: 1,
      total: 100,
      pageSize: 10,
      songList: [],
      playlists: [],
      mvs: [],
      query: this.$route.query.q,
      type: 1,
    };
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.page = val;
    },
    toMv(id) {
      this.$router.push(`/mv?id=${id}`);
    },
    loadData() {
      axios({
        url: "/autumnfish/search",
        method: "get",
        params: {
          keywords: this.query,
          type: this.type,
          limit: this.pageSize,
          offset: (this.page - 1) * this.pageSize,
        },
      }).then((res) => {
        switch (this.type) {
          // 歌曲
          case 1:
            this.songList = res.data.result.songs;
            this.total = res.data.result.songCount;
            for (let i = 0; i < this.songList.length; i++) {
              let minute = parseInt(this.songList[i].duration / 1000 / 60);
              let second = parseInt((this.songList[i].duration / 1000) % 60);

              if (minute < 10) {
                minute = "0" + minute;
              }

              if (second < 10) {
                second = "0" + second;
              }

              this.songList[i].duration = `${minute}:${second}`;
            }
            break;

          // 歌单
          case 1000:
            this.playlists = res.data.result.playlists;
            this.total = res.data.result.playlistCount;
            break;

          // MV
          case 1004:
            this.mvs = res.data.result.mvs;
            this.total = res.data.result.mvCount;
            for (let i = 0; i < this.mvs.length; i++) {
              let minute = parseInt(this.mvs[i].duration / 1000 / 60);
              if (minute < 10) {
                minute = "0" + minute;
              }

              let secod = parseInt((this.mvs[i].duration / 1000) % 60);
              if (secod < 10) {
                secod = "0" + secod;
              }

              this.mvs[i].duration = `${minute}:${secod}`;
            }
            break;

          default:
            break;
        }
      });
    },
    play(id) {
      axios({
        url: "autumnfish/song/url",
        method: "get",
        params: {
          id,
        },
      }).then((res) => {
        let url = res.data.data[0].url;
        if (!url) {
          this.$message.error("无法播放这首歌，可能因为没有版权");
        }
        this.$parent.musicUrl = url;
      });
    },
    toMusicList(id) {
      this.$router.push(`/musicList?id=${id}`);
    },
  },
  watch: {
    page() {
      this.loadData();
    },
    query() {
      this.loadData();
    },
    $route() {
      this.query = this.$route.query.q;
      this.page = 1;
    },
    activeIndex() {
      // song 歌曲
      // lists 歌单
      // mv MV
      switch (this.activeIndex) {
        case "songs":
          this.type = 1;
          this.pageSize = 10;
          break;

        case "lists":
          this.type = 1000;
          this.pageSize = 15;
          break;

        case "mv":
          this.type = 1004;
          this.pageSize = 8;
          break;

        default:
          break;
      }
    },
    type() {
      this.loadData();
    },
  },
  created() {
    this.loadData();
  },
};
</script>

<style >
</style>
